package com.kaoshi.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaoshi.common.config.Result;
import com.kaoshi.entity.Institution;
import com.kaoshi.service.IInstitutionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import com.kaoshi.entity.Institution;
import com.kaoshi.service.IInstitutionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-29
 */
@RestController
@CrossOrigin
@RequestMapping("/institution")
public class InstitutionController {
    @Autowired
    private IInstitutionService institutionService;

    @RequestMapping("findpage")
    public Result findpage(Integer current, Integer pageSize, @RequestBody Institution institution) {
        Page<Institution> institutionPage = new Page<>(current, pageSize);
        LambdaQueryWrapper<Institution> qw = new LambdaQueryWrapper<>();
        qw.eq(Institution::getDel, 1);
        if (institution.getOrganization() != null && institution.getOrganization() != "") {
            qw.like(Institution::getOrganization, institution.getOrganization());
        }
        return Result.ok(institutionService.page(institutionPage, qw));
    }

    @RequestMapping("save")
    public Result save(@RequestBody Institution institution) {
        if (institution.getId() == null) {
            institutionService.save(institution);
            return Result.ok();
        } else {
            institutionService.updateById(institution);
            return Result.ok();
        }
    }

    @RequestMapping("queryById")
    public Result queryById(Integer id) {
        return Result.ok(institutionService.getById(id));
    }

    @RequestMapping("dc")
    public void dc(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("excel", "UTF-8").replaceAll("/+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        //2.查询要导出的数据
        List<Institution> studentList = institutionService.list();
        for (Institution institution : studentList) {

            String introduce = institution.getImg();
            String replace = introduce.replace("https://spyx-h2310a.oss-cn-beijing.aliyuncs.com/", "");
            byte[] imgBytes = OssImageUtils.getImageBytesFromOss("spyx-h2310a", replace);
            institution.setImgBytes(imgBytes);

        }

        //3.使用EasyExcel完成导出
        EasyExcel.write(response.getOutputStream())
                .head(Institution.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("表格")
                .doWrite(studentList);
    }

    @RequestMapping("findAll")
    public List<Institution> findAll(){
        return institutionService.list();
    }
}
